﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;


namespace Hisun.View.Management
{
    public partial class UserRightSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int pageIndx = Convert.ToInt32(CurrentPage.Value);
                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals /10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0);

            
            
            }

        }

        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            var MyQuery = (from a in db.TSCRB_JS_Users select a).Skip(skipAccount).Take(10);
            GridView_Users.DataSource = MyQuery;
            GridView_Users.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TSCRB_JS_Users select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        pageIndx = Convert.ToInt32(TextBox_PageIndex.Text) - 1;
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }

        #endregion

        protected void Button_ShowDetailRight_Command(object sender, CommandEventArgs e)
        {
            string myUserId = e.CommandArgument.ToString();
            Response.Redirect("UserRightSetupSelectRight.aspx?userid=" + myUserId);
        }

        protected void Button_ShowRolePage_Command(object sender, CommandEventArgs e)
        {
            string myUserId = e.CommandArgument.ToString();
            Response.Redirect("UserRightSetupSelectRole.aspx?userid=" + myUserId);
        }


       //同步操作员列系统表到日报
        protected void Button_Refresh_Click(object sender, EventArgs e)
        {
            var AbcUserList = from a in db.TAbc_JS_Users
                              select a;
            if (AbcUserList.Count() > 0)
            {
                try
                {
                    foreach (var AbcUser in AbcUserList)
                    {
                        var ScrbUser = from a in db.TSCRB_JS_Users
                                       where a.操作员ID == AbcUser.操作员ID
                                       select a;
                        if (ScrbUser.Count() == 1)
                        {


                        }
                        else
                        {

                            TSCRB_JS_Users AddRow = new TSCRB_JS_Users();
                            AddRow.公司ID = AbcUser.公司ID;
                            AddRow.操作员ID = AbcUser.操作员ID;
                            AddRow.操作员姓名 = AbcUser.操作员姓名;
                            AddRow.创建时间 = AbcUser.创建时间;
                            AddRow.停用 = AbcUser.停用;
                            AddRow.责任部门ID = AbcUser.责任部门ID;
                            AddRow.编辑人ID = AbcUser.编辑人ID;
                            AddRow.修改时间 = AbcUser.修改时间;

                            db.TSCRB_JS_Users.InsertOnSubmit(AddRow);

                            db.SubmitChanges();

                        }


                    }
                    alert = "<script>alert('刷新成功');</script>";
                }
                catch { alert = "<script>alert('刷新失败');</script>"; }
               


           



            }
        }
    }
}
